
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>类的特殊方法 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="异常处理.html" />
    
    
    <link rel="prev" href="面向对象编程.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >类的特殊方法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7C7B;&#x7684;&#x7279;&#x6B8A;&#x65B9;&#x6CD5;">&#x7C7B;&#x7684;&#x7279;&#x6B8A;&#x65B9;&#x6CD5;</h1>
<hr>
<p>Python &#x7684; class &#x4E2D;&#x8FD8;&#x6709;&#x8BB8;&#x591A;&#x8FD9;&#x79CD;&#x5F62;&#x5982;<code>__xxx__</code>&#x7684;&#x53D8;&#x91CF;&#x6216;&#x8005;&#x51FD;&#x6570;&#x540D;&#xFF0C;&#x5B83;&#x4EEC;&#x6709;&#x7279;&#x6B8A;&#x7528;&#x9014;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x5B9A;&#x5236;&#x7C7B;&#x3002;</p>
<h2 id="len"><code>__len__</code></h2>
<p>&#x5982;&#x679C;&#x4F60;&#x8C03;&#x7528; <code>len()</code> &#x51FD;&#x6570;&#x8BD5;&#x56FE;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x5728; <code>len()</code> &#x51FD;&#x6570;&#x5185;&#x90E8;&#xFF0C;&#x5B83;&#x81EA;&#x52A8;&#x53BB;&#x8C03;&#x7528;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;<code>__len__()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>len(<span class="hljs-string">&apos;ABC&apos;</span>)
<span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;ABC&apos;</span>.__len__()
<span class="hljs-number">3</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x5199;&#x7684;&#x7C7B;&#xFF0C;&#x5982;&#x679C;&#x4E5F;&#x60F3;&#x7528; <code>len(myObj)</code>&#x7684;&#x8BDD;&#xFF0C;&#x5C31;&#x81EA;&#x5DF1;&#x5199;&#x4E00;&#x4E2A;<code>__len__()</code>&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>:</span>
<span class="hljs-meta">... </span>  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__len__</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-meta">... </span>     <span class="hljs-keyword">return</span> <span class="hljs-number">100</span>
...
<span class="hljs-meta">&gt;&gt;&gt; </span>dog = Dog()
<span class="hljs-meta">&gt;&gt;&gt; </span>len(dog)
<span class="hljs-number">100</span>
</code></pre>
<h2 id="str"><code>__str__</code></h2>
<p>print&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x65F6;&#xFF0C;&#x81EA;&#x5B9A;&#x4E49;&#x6253;&#x5370;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Student object&apos;</span>
s = Student()
print(s)   <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;   Student object</span>
</code></pre>
<p>&#x4F46;&#x662F;&#x5728;&#x7EC8;&#x7AEF;&#x4E2D;&#x76F4;&#x63A5;&#x663E;&#x793A;&#x53D8;&#x91CF;&#x4E0D;&#x8D77;&#x4F5C;&#x7528;&#xFF0C;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x76F4;&#x63A5;&#x663E;&#x793A;&#x53D8;&#x91CF;&#x8C03;&#x7528;&#x7684;&#x4E0D;&#x662F;<code>__str__()</code>&#xFF0C;&#x800C;&#x662F;<code>__repr__()</code>&#x3002;&#x4E24;&#x8005;&#x7684;&#x533A;&#x522B;&#x662F;<code>__str__()</code>&#x8FD4;&#x56DE;&#x7528;&#x6237;&#x770B;&#x5230;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x800C;<code>__repr__()</code>&#x8FD4;&#x56DE;&#x7A0B;&#x5E8F;&#x5F00;&#x53D1;&#x8005;&#x770B;&#x5230;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C; <code>__repr__()</code>&#x662F;&#x4E3A;&#x8C03;&#x8BD5;&#x670D;&#x52A1;&#x7684;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>s
&lt;__main__.Student object at <span class="hljs-number">0x109afb310</span>&gt;
</code></pre>
<p>&#x89E3;&#x51B3;&#x529E;&#x6CD5;&#x662F;&#x518D;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;<code>__repr__()</code>&#x3002;&#x4F46;&#x662F;&#x901A;&#x5E38;<code>__str__()</code>&#x548C;<code>__repr__()</code>&#x4EE3;&#x7801;&#x90FD;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x6709;&#x4E2A;&#x5077;&#x61D2;&#x7684;&#x5199;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Student object&apos;</span>
    __repr__ = __str__
</code></pre>
<h2 id="iter"><code>__iter__</code></h2>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7C7B;&#x60F3;&#x88AB;&#x7528;&#x4E8E; <code>for ... in</code> &#x5FAA;&#x73AF;&#xFF0C;&#x7C7B;&#x4F3C; list &#x6216; tuple &#x90A3;&#x6837;&#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;<code>__iter__()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF08;Iterator&#xFF09;&#xFF0C;&#x7136;&#x540E;&#xFF0C; Python &#x7684; for&#x5FAA;&#x73AF;&#x5C31;&#x4F1A;&#x4E0D;&#x65AD;&#x8C03;&#x7528;&#x8BE5;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;<code>__next__()</code>&#x65B9;&#x6CD5;&#x62FF;&#x5230;&#x5FAA;&#x73AF;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x76F4;&#x5230;&#x9047;&#x5230; StopIteration &#x9519;&#x8BEF;&#x65F6;&#x9000;&#x51FA;&#x5FAA;&#x73AF;&#x3002;<br>&#x6211;&#x4EEC;&#x4EE5;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x4E3A;&#x4F8B;&#xFF0C;&#x5199;&#x4E00;&#x4E2A; Fib &#x7C7B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F5C;&#x7528;&#x4E8E; for &#x5FAA;&#x73AF;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Fib</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        self.a, self.b = <span class="hljs-number">0</span>, <span class="hljs-number">1</span>    <span class="hljs-comment"># &#x521D;&#x59CB;&#x5316;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self              <span class="hljs-comment"># &#x5B9E;&#x4F8B;&#x672C;&#x8EAB;&#x5C31;&#x662F;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF0C;&#x6545;&#x8FD4;&#x56DE;&#x81EA;&#x5DF1;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__next__</span><span class="hljs-params">(self)</span>:</span>
        self.a, self.b = self.b, self.a + self.b    
        <span class="hljs-keyword">if</span> self.a &gt; <span class="hljs-number">100000</span>:      <span class="hljs-comment"># &#x9000;&#x51FA;&#x5FAA;&#x73AF;&#x7684;&#x6761;&#x4EF6;</span>
            <span class="hljs-keyword">raise</span> StopIteration();
        <span class="hljs-keyword">return</span> self.a            <span class="hljs-comment"># &#x8FD4;&#x56DE;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;</span>
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x8BD5;&#x8BD5;&#x628A; Fib &#x5B9E;&#x4F8B;&#x4F5C;&#x7528;&#x4E8E; for &#x5FAA;&#x73AF;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> Fib():
<span class="hljs-meta">... </span>print(n)
</code></pre>
<h2 id="next"><code>__next__</code></h2>
<blockquote>
<p>&#x6709;&#x4E9B;&#x5BF9;&#x8C61;&#x91CC;&#x5305;&#x542B;&#x5BF9;&#x5176;&#x4ED6;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#xFF1A;&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#x79F0;&#x4E3A;&#x5BB9;&#x5668;&#x3002;Python&#x4E2D;&#x7684;&#x5BB9;&#x5668;&#xFF0C;&#x4F8B;&#x5982;list&#x7B49;&#x3002;</p>
</blockquote>
<p><code>__next__</code>&#x7528;&#x4E8E;&#x5BB9;&#x5668;&#x4E2D;&#x7684;&#x8FED;&#x4EE3;&#xFF0C;&#x5BB9;&#x5668;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x4EE5;&#x63D0;&#x4F9B;&#x8FED;&#x4EE3;&#x652F;&#x6301;&#xFF1A;  </p>
<pre><code>Container.__iter__()    #&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x5BF9;&#x8C61;&#x8981;&#x6C42;&#x652F;&#x6301;&#x540E;&#x9762;&#x4ECB;&#x7ECD;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#x534F;&#x8BAE;&#x3002;
</code></pre><p><strong>&#x8FED;&#x4EE3;&#x534F;&#x8BAE;</strong><br>&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x88AB;&#x8981;&#x6C42;&#x652F;&#x6301;&#x4E0B;&#x9762;&#x7684;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x5408;&#x8D77;&#x6765;&#x5F62;&#x6210;&#x8FED;&#x4EE3;&#x534F;&#x8BAE;&#x3002;  </p>
<pre><code>Iterator.__iter__()  #&#x8FD4;&#x56DE;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x81EA;&#x8EAB;&#x3002;&#x4E3A;&#x4E86;&#x5141;&#x8BB8;&#x5BB9;&#x5668;&#x548C;&#x8FED;&#x4EE3;&#x5668;&#x88AB;&#x7528;&#x4E8E;for&#x548C;in&#x8BED;&#x53E5;&#x4E2D;&#xFF0C;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x8BE5;&#x65B9;&#x6CD5;&#x3002;
Iterator.__next__()  #&#x8FD4;&#x56DE;&#x5BB9;&#x5668;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x6761;&#x76EE;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x66F4;&#x591A;&#x7684;&#x6761;&#x76EE;&#xFF0C;&#x629B;&#x51FA;StopIteration&#x5F02;&#x5E38;&#x3002;
</code></pre><p><strong>&#x624B;&#x52A8;&#x8FED;&#x4EE3;</strong><br>&#x5185;&#x7F6E;&#x51FD;&#x6570;iter&#xFF0C;&#x5B83;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<code>__iter__</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x83B7;&#x5F97;&#x4E00;&#x4E2A;&#x8FED;&#x4EE3;&#x5668;&#x3002;<br>&#x5185;&#x7F6E;&#x51FD;&#x6570;next&#xFF0C;&#x5B83;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<code>__next__</code>&#x65B9;&#x6CD5;&#xFF0C;&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;X&#xFF0C;&#x81EA;&#x8EAB;&#x6709;<code>__next__</code>&#x65B9;&#x6CD5;&#xFF0C;&#x8C03;&#x7528;<code>next(X)</code>&#x7B49;&#x540C;&#x4E8E;<code>X.__next__()</code>&#xFF0C;&#x4F46;&#x524D;&#x8005;&#x7B80;&#x4FBF;&#x5F88;&#x591A;&#x3002;  </p>
<p>&#x5217;&#x8868;&#x7B49;&#x7684;&#x5185;&#x7F6E;&#x5BF9;&#x8C61;&#xFF0C;&#x81EA;&#x8EAB;&#x4E0D;&#x662F;&#x8FED;&#x4EE3;&#x5668;&#xFF08;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;<code>__next__</code>&#x65B9;&#x6CD5;&#xFF09;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x9700;&#x8981;&#x652F;&#x6301;&#x591A;&#x6B21;&#x8FED;&#x4EE3;&#x3002;&#x5BF9;&#x4E8E;&#x8FD9;&#x6837;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x8C03;&#x7528;iter&#x6765;&#x542F;&#x52A8;&#x8FED;&#x4EE3;&#x3002;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>L = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>I = iter(L)
<span class="hljs-meta">&gt;&gt;&gt; </span>I.__next__()
<span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>next(I)
<span class="hljs-number">2</span>
</code></pre>
<h2 id="getitem"><code>__getitem__</code></h2>
<p>Fib &#x5B9E;&#x4F8B;&#x867D;&#x7136;&#x80FD;&#x4F5C;&#x7528;&#x4E8E; for &#x5FAA;&#x73AF;&#xFF0C;&#x770B;&#x8D77;&#x6765;&#x548C; list &#x6709;&#x70B9;&#x50CF;&#xFF0C;&#x4F46;&#x662F;&#xFF0C;&#x628A;&#x5B83;&#x5F53;&#x6210;list &#x6765;&#x4F7F;&#x7528;&#x8FD8;&#x662F;&#x4E0D;&#x884C;&#xFF0C;&#x6BD4;&#x5982;&#xFF0C;&#x53D6;&#x7B2C; 5 &#x4E2A;&#x5143;&#x7D20;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>Fib()[<span class="hljs-number">5</span>]
Traceback (most recent call last):
File <span class="hljs-string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: <span class="hljs-string">&apos;Fib&apos;</span> object does <span class="hljs-keyword">not</span> support indexing
</code></pre>
<p>&#x8981;&#x8868;&#x73B0;&#x5F97;&#x50CF; list &#x90A3;&#x6837;&#x6309;&#x7167;&#x4E0B;&#x6807;&#x53D6;&#x51FA;&#x5143;&#x7D20;&#xFF0C;&#x9700;&#x8981;&#x5B9E;&#x73B0;<code>__getitem__()</code>&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__getitem__</span><span class="hljs-params">(self, index)</span>:</span>
    a, b = <span class="hljs-number">1</span>, <span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(index):
        a, b = b, a + b
    <span class="hljs-keyword">return</span> a
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x6309;&#x4E0B;&#x6807;&#x8BBF;&#x95EE;&#x6570;&#x5217;&#x7684;&#x4EFB;&#x610F;&#x4E00;&#x9879;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>f = Fib()
<span class="hljs-meta">&gt;&gt;&gt; </span>f[<span class="hljs-number">0</span>]
<span class="hljs-number">1</span>
</code></pre>
<p>&#x4F46;&#x662F; list&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x5207;&#x7247;&#x65B9;&#x6CD5;&#xFF0C;<code>__getitem__()</code>&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x53EF;&#x80FD;&#x662F;&#x4E00;&#x4E2A; int&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x4E00;&#x4E2A;&#x5207;&#x7247;&#x5BF9;&#x8C61; slice&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x505A;&#x5224;&#x65AD;&#x3002;&#x8FD8;&#x9700;&#x8981;&#x5904;&#x7406;step&#x3001;&#x8D1F;&#x6570;&#x4E0B;&#x6807;&#x7B49;&#x7B49;&#x3002;&#x8981;&#x6B63;&#x786E;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;<code>__getitem__()</code>&#x8FD8;&#x662F;&#x6709;&#x5F88;&#x591A;&#x5DE5;&#x4F5C;&#x8981;&#x505A;&#x7684;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x4E0E;<code>__getitem__</code>&#x5BF9;&#x5E94;&#x7684;&#x662F;<code>__setitem__()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x628A;&#x5BF9;&#x8C61;&#x89C6;&#x4F5C; list &#x6216; dict &#x4E00;&#x6837;&#x6765;&#x5BF9;&#x96C6;&#x5408;&#x8D4B;&#x503C;&#x3002;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;<code>__delitem__()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x4E8E;&#x5220;&#x9664;&#x67D0;&#x4E2A;&#x5143;&#x7D20;&#x3002;</p>
<blockquote>
<p>&#x603B;&#x4E4B;&#xFF0C;&#x901A;&#x8FC7;&#x4E0A;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x8868;&#x73B0;&#x5F97;&#x548C; Python &#x81EA;&#x5E26;&#x7684; list&#x3001;tuple&#x3001; dict &#x6CA1;&#x4EC0;&#x4E48;&#x533A;&#x522B;&#xFF0C;&#x8FD9;&#x5B8C;&#x5168;&#x5F52;&#x529F;&#x4E8E;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x201C;&#x9E2D;&#x5B50;&#x7C7B;&#x578B;&#x201D;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x5F3A;&#x5236;&#x7EE7;&#x627F;&#x67D0;&#x4E2A;&#x63A5;&#x53E3;&#x3002;</p>
</blockquote>
<h2 id="getattr"><code>__getattr__</code></h2>
<p>&#x5728;&#x8BBF;&#x95EE;&#x5B9E;&#x4F8B;&#x7684;&#x5C5E;&#x6027;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5982;&#x679C;&#x8BE5;&#x5C5E;&#x6027;&#x4E0D;&#x5B58;&#x5728;&#x5219;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;<code>__getattr__</code>&#x6765;&#x7EE7;&#x7EED;&#x67E5;&#x627E;&#x3002;<br>&#x8981;&#x8BA9;class&#x53EA;&#x54CD;&#x5E94;&#x7279;&#x5B9A;&#x7684;&#x51E0;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x8981;&#x6309;&#x7167;&#x7EA6;&#x5B9A;&#xFF0C;&#x629B;&#x51FA; AttributeError &#x7684;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__getattr__</span><span class="hljs-params">(self, attr)</span>:</span>
        <span class="hljs-keyword">if</span> attr == <span class="hljs-string">&apos;age&apos;</span>:
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">lambda</span>:<span class="hljs-number">25</span>
        <span class="hljs-keyword">raise</span> AttributeError(<span class="hljs-string">&quot;Student&apos;s object has no attribute %s&quot;</span> % attr)
</code></pre>
<p>&#x8FD9;&#x5B9E;&#x9645;&#x4E0A;&#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x5168;&#x90E8;&#x52A8;&#x6001;&#x5316;&#x5904;&#x7406;&#x4E86;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x4EFB;&#x4F55;&#x7279;&#x6B8A;&#x7684;&#x624B;&#x6BB5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Gen</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, root_url=<span class="hljs-string">&apos;&apos;</span>)</span>:</span>
        self.url = root_url
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__getattr__</span><span class="hljs-params">(self, item)</span>:</span>
        <span class="hljs-keyword">if</span> item == <span class="hljs-string">&apos;get&apos;</span> <span class="hljs-keyword">or</span> item == <span class="hljs-string">&apos;post&apos;</span>:
            <span class="hljs-keyword">return</span> self.url
        <span class="hljs-keyword">return</span> Gen(<span class="hljs-string">&apos;{}/{}&apos;</span>.format(self.url, item))
gen = Gen(<span class="hljs-string">&apos;http://&apos;</span>)
s = gen.users.show.erick.com.get    <span class="hljs-comment">#&#x94FE;&#x5F0F;&#x8C03;&#x7528;</span>
print(s)   <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;http://users.show.erick.com</span>
</code></pre>
<p>&#x5145;&#x5206;&#x5229;&#x7528;<code>__getattr__</code>&#x4F1A;&#x5728;&#x6CA1;&#x6709;&#x67E5;&#x627E;&#x5230;&#x76F8;&#x5E94;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x65F6;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x7279;&#x70B9;&#xFF0C;&#x65B9;&#x4FBF;&#x7684;&#x901A;&#x8FC7;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x751F;&#x6210;&#x5BF9;&#x5E94;&#x7684;url&#x3002;</p>
<h2 id="call"><code>__call__</code></h2>
<p>&#x5F53;&#x6211;&#x4EEC;&#x8C03;&#x7528;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x7528; <code>&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;.&#x65B9;&#x6CD5;()</code> &#x5F62;&#x5F0F;&#x6765;&#x8C03;&#x7528;&#x3002;<br>&#x5982;&#x679C;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;<code>__call__()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5BF9;&#x5B9E;&#x4F8B;&#x8FDB;&#x884C;&#x8C03;&#x7528;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span>
        self.name = name
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span><span class="hljs-params">(self)</span>:</span>   <span class="hljs-comment">#&#x6CE8;&#xFF1A;__call__()&#x8FD8;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x53C2;&#x6570;&#x3002;</span>
        print(<span class="hljs-string">&apos;My name is %s.&apos;</span> % self.name)
</code></pre>
<p>&#x8C03;&#x7528;&#x65B9;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>s = Student(<span class="hljs-string">&apos;Michael&apos;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>s()     <span class="hljs-comment"># self &#x53C2;&#x6570;&#x4E0D;&#x8981;&#x4F20;&#x5165;</span>
My name <span class="hljs-keyword">is</span> Michael.
</code></pre>
<p>&#x8FD9;&#x4E48;&#x4E00;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x6A21;&#x7CCA;&#x4E86;&#x5BF9;&#x8C61;&#x548C;&#x51FD;&#x6570;&#x7684;&#x754C;&#x9650;&#x3002;<br>&#x90A3;&#x4E48;&#xFF0C;&#x600E;&#x4E48;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5BF9;&#x8C61;&#x8FD8;&#x662F;&#x51FD;&#x6570;&#x5462;&#xFF1F;&#x5176;&#x5B9E;&#xFF0C;&#x66F4;&#x591A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x80FD;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x80FD;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x4E00;&#x4E2A; Callable &#x5BF9;&#x8C61;&#xFF0C;&#x6BD4;&#x5982;&#x51FD;&#x6570;&#x548C;&#x6211;&#x4EEC;&#x4E0A;&#x9762;&#x5B9A;&#x4E49;&#x7684;&#x5E26;&#x6709;<code>__call__()</code>&#x7684;&#x7C7B;&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>callable(Student())
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>callable(max)
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>callable([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
<span class="hljs-keyword">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>callable(<span class="hljs-keyword">None</span>)
<span class="hljs-keyword">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>callable(<span class="hljs-string">&apos;str&apos;</span>)
<span class="hljs-keyword">False</span>
</code></pre>
<p>&#x901A;&#x8FC7; callable()&#x51FD;&#x6570;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x201C;&#x53EF;&#x8C03;&#x7528;&#x201D;&#x5BF9;&#x8C61;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="面向对象编程.html" class="navigation navigation-prev " aria-label="Previous page: 面向对象编程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="异常处理.html" class="navigation navigation-next " aria-label="Next page: 异常处理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"类的特殊方法","level":"1.9","depth":1,"next":{"title":"异常处理","level":"1.10","depth":1,"path":"异常处理.md","ref":"异常处理.md","articles":[]},"previous":{"title":"面向对象编程","level":"1.8","depth":1,"path":"面向对象编程.md","ref":"面向对象编程.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"类的特殊方法.md","mtime":"2017-08-23T14:50:53.119Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

